package com.oyjd.business.handler.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.oyjd.R;

/**
 * Handler使用例子
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class HandlerDemo extends Activity {

	private TextView tv;
	
	private static Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Tmp t = (Tmp) msg.obj;
			t.getTv().setText(t.getTime());
		}

	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_show_txt_demo);
		tv = (TextView) this.findViewById(R.id.tv_content);
		//
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					Calendar c = Calendar.getInstance();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
					Tmp t = new Tmp();
					t.setTime(sdf.format(c.getTime()));
					t.setTv(tv);
					Message msg = new Message();
					msg.obj = t;
					handler.sendMessage(msg);
					try {
						Thread.sleep(1000L);
					} catch (InterruptedException e) {

					}
				}

			}
		}).start();
	}

	private class Tmp {
		private TextView tv;
		private String time;

		public TextView getTv() {
			return tv;
		}

		public void setTv(TextView tv) {
			this.tv = tv;
		}

		public String getTime() {
			return time;
		}

		public void setTime(String time) {
			this.time = time;
		}

	}

}
